//
//  JsWebImageMemoryCache.m
//  JsoUtilitySdk
//
//  Created by Gikki Ares on 2021/8/12.
//

#import "JsWebImageMemoryCache.h"

@interface JsWebImageMemoryCache ()

@property(nonatomic,strong)NSCache * cache;

@end

@implementation JsWebImageMemoryCache

static JsWebImageMemoryCache * _sharedInstance;
+ (JsWebImageMemoryCache * )sharedInstance {
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken,^{
		_sharedInstance = [JsWebImageMemoryCache new];
	});
	return _sharedInstance;
}

- (instancetype)init {
	if(self = [super init]) {
		self.cache = [NSCache new];
		//最多缓存50个图片.
		self.cache.countLimit = 50;
		//最多缓存20Mb文件
		self.cache.totalCostLimit = 20 * 1024 * 1024;
	}
	return self;
}

-(UIImage *)imageForKey:(NSString *)key {
	return [self.cache objectForKey:key];
}

- (void)cacheImage:(UIImage *)image forKey:(NSString *)key {
	[self.cache setObject:image forKey:key];
}

@end
